package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;
    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

	//专辑分类
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//这个是整体分类
		List<JSONObject> jsonObjectList1 = new ArrayList<>();
		//是数据库中的全部分类
		List<BaseCategoryView> baseCategoryViewsList = baseCategoryViewMapper.selectList(null);
		//1.从全部分类中获取key为1组的id
		Map<Long,List<BaseCategoryView>> category1Map= baseCategoryViewsList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));//key:value,key为getCategory1Id
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
			JSONObject jsonObject1 = new JSONObject();
			Long category1Id = entry1.getKey();
			String category1Name = entry1.getValue().get(0).getCategory1Name();
			jsonObject1.put("categoryId",category1Id);
			jsonObject1.put("categoryName",category1Name);

			List<JSONObject> jsonObjectList2 = new ArrayList<>();
			//对"1级"分类集合按照二级分类ID进行分组
			Map<Long,List<BaseCategoryView>> category2Map =entry1.getValue().stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				Long category2Id = entry2.getKey();
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				jsonObject2.put("categoryId",category2Id);
				jsonObject2.put("categoryName",category2Name);
				jsonObjectList2.add(jsonObject2);
				List<JSONObject> jsonObjectList3 = new ArrayList<>();
				for (BaseCategoryView entry3 : entry2.getValue()) {
					JSONObject jsonObject3 = new JSONObject();
					Long category3Id = entry3.getCategory3Id();
					String category3Name = entry3.getCategory3Name();
					jsonObject3.put("categoryId",category3Id);
					jsonObject3.put("categoryName",category3Name);
					jsonObjectList3.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",jsonObjectList3);
			}
			jsonObject1.put("categoryChild",jsonObjectList2);
			jsonObjectList1.add(jsonObject1);
		}
		return jsonObjectList1;

		/*//1.创建响应结果集合对象-用于封装所有一级分类对象
		List<JSONObject> returnList = new ArrayList<>();
		//2.查询所有分类数据-查询视图即可 共计401条记录
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		//3.处理一级分类数据
		//3.1 对所有分类集合列表进行分组按照1级分类ID进行分组
								// 得到Map<分组ID，一级分类列表>
		Map<Long,List<BaseCategoryView>> category1Map = allCategoryList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历
		for (Map.Entry<Long, List<BaseCategoryView>>entry1 : category1Map.entrySet()){
			//3.2 封装一级分类对象
			JSONObject jsonObject1 = new JSONObject();
			//3.2.1 封装1级分类ID
			Long category1Id = entry1.getKey();
			jsonObject1.put("categoryId", category1Id);
			//3.2.2 封装1级分类名称
			String category1Name = entry1.getValue().get(0).getCategory1Name();
			jsonObject1.put("categoryName", category1Name);
			//4. 处理二级分类数据
			List<JSONObject> jsonObjects2List = new ArrayList<>();
			//4.1 对"1级"分类集合按照二级分类ID进行分组
			Map<Long,List<BaseCategoryView>> category2Map = entry1.getValue()
							.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//4.2 遍历"2级"分类Map
			for (Map.Entry<Long,List<BaseCategoryView>> entry2:category2Map.entrySet()){
				//4.3 封装二级分类对象
				JSONObject jsonObject2 = new JSONObject();
				//4.3.1 封装2级分类ID
				Long category2Id = entry2.getKey();
				jsonObject2.put("categoryId",category2Id);
				//4.3.2 封装2级分类名称
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				jsonObject2.put("categoryName",category2Name);
				//4.4 将2级分类对象放入二级分类集合中
				jsonObjects2List.add(jsonObject2);
				//5. 处理三级分类数据
				List<JSONObject> jsonObjectList3 = new ArrayList<>();
				//5.1 对"2级"分类列表进行遍历
				for (BaseCategoryView baseCategoryView:entry2.getValue()){
					//5.2 封装三级分类JSONOBject对象
					JSONObject jsonObject3 = new JSONObject();

					Long category3Id = baseCategoryView.getCategory3Id();
					jsonObject3.put("categoryId",category3Id);

					String category3Name = baseCategoryView.getCategory3Name();
					jsonObject3.put("categoryName",category3Name);

					//5.3 将3级分类对象放入集合中
					jsonObjectList3.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",jsonObjectList3);
			}
			jsonObject1.put("categoryChild",jsonObjects2List);
			returnList.add(jsonObject1);
		}
		//全部试图
		return returnList;*/
	}

	//根据一级分类Id获取分类属性以及属性值（标签名，标签值）列表
	@Override
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
	//1.获取持久层接口，调用持久层动态SQL
		return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {

		return baseCategoryViewMapper.selectById(category3Id);
	}


	/**
	 * 根据一级分类Id查询置顶7个三级分类列表
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {

		//1.根据1级分类ID查询该1级分类下二级分类ID列表
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
				new LambdaQueryWrapper<BaseCategory2>()
						.eq(BaseCategory2::getCategory1Id, category1Id)
						.select(BaseCategory2::getId)
		);
		if (CollUtil.isNotEmpty(baseCategory2List)) {
			List<Long> category2IdList = baseCategory2List.stream()
					.map(BaseCategory2::getId).collect(Collectors.toList());
			//2.根据二级分类ID列表查询置顶前7个个三级分类列表
			LambdaQueryWrapper<BaseCategory3> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.in(BaseCategory3::getCategory2Id, category2IdList);
			queryWrapper.eq(BaseCategory3::getIsTop, 1);
			queryWrapper.orderByAsc(BaseCategory3::getOrderNum);
			queryWrapper.last("limit 7");
			return baseCategory3Mapper.selectList(queryWrapper);

		}

		return null;
	}

	/**
	 * 查询1级分类下包含所有二级以及三级分类
	 *
	 * @param category1Id
	 * @return 一级分类对象
	 */
	@Override
	public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {

		//1.根据1id查视图
		List<BaseCategoryView> baseCategory1List = baseCategoryViewMapper.selectList(
				new LambdaQueryWrapper<BaseCategoryView>()
						.eq(BaseCategoryView::getCategory1Id, category1Id)
		);
		//非空
		if (CollUtil.isNotEmpty(baseCategory1List)){
			BaseCategoryView baseCategoryView = baseCategory1List.get(0);
			//1.2 创建1级分类JSON对象
			JSONObject jsonObject1 = new JSONObject();
			//1.2.1 封装1级分类ID
			jsonObject1.put("categoryId", baseCategoryView.getCategory1Id());
			//1.2.2 封装1级分类名称
			jsonObject1.put("categoryName", baseCategoryView.getCategory1Name());
			//2.
			//创建2json对象
			List<JSONObject> jsonObject2List = new ArrayList<>();
			// 对"1级分类列表"按二级分类ID进行分组，得到Map2
			Map<Long, List<BaseCategoryView>> map2 = baseCategory1List
					.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			// 遍历Map2，创建二级分类JSON对象集合
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
				//2.2.1 创建二级分类JSON对象
				JSONObject jsonObject2 = new JSONObject();
				//2.2.2 封装二级分类ID,名称
				Long category2Id = entry2.getKey();
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				jsonObject2.put("categoryId", category1Id);
				jsonObject2.put("categoryName", category2Name);
				//3
				List<JSONObject> jsonObject3List = new ArrayList<>();
				for (BaseCategoryView categoryView : entry2.getValue()) {
					JSONObject jsonObject3 = new JSONObject();
					//3.2.2 封装3级分类ID
					jsonObject3.put("categoryId", categoryView.getCategory3Id());
					//3.2.3 封装3级分类名称
					jsonObject3.put("categoryName", categoryView.getCategory3Name());
					jsonObject3List.add(jsonObject3);
				}
				//2.3 将三级分类集合加入到二级分类对象"categoryChild"中
				jsonObject2.put("categoryChild", jsonObject3List);
				jsonObject2List.add(jsonObject2);
			}
			//2.3 将二级分类集合加入到一级分类对象"categoryChild"中
			jsonObject1.put("categoryChild", jsonObject2List);
			return jsonObject1;
		}
		return null;
	}


}
